#ifndef BASICS_VMAP_H
#define BASICS_VMAP_H
//#include "stdafx.h"
using namespace std;

class Basics_vmap
{
public:
	template <typename T,typename U> static int set_vals_from_row_vector(map<T,vector<U> > &target,vector <pair <T,U> > &source,unsigned int row)
	{
		typename map<T,vector <U> >::const_iterator it;
		unsigned int col;
		for(col=0;col<source.size();col++){
			it=target.find(source[col].first);
			if(it!=target.end()){
				if(row<it->second.size()){
					target[it->first][row]=source[col].second;
				}
				else{
					err::err3("Basics_vmap::set_vals_from_row_vector: Actually, the vectors of the target map are to small");
				}
			}
		}
		return 0;
	}
	static int read_vector_map (string root, string name,string suffix,map <const string,vector <double> > mymap)
	{
		int col,ncols,count;
		double dummy;
		vector<string> var_id;
		string file;
		string descript;
		file= root+name+suffix;

		// clear the map
		mymap.clear();

		//start to read
		ifstream in(file.c_str());
		if(!in)
		{
				return 1;
				cout<<"Basic_maps::read_vector_map: File not found: "<<file<<endl;
		}
		in>>ncols;
		for(col=0;col<ncols;col++)
		{
			in>>descript;
			var_id.push_back(descript);
		}
		count=0;
		while(!in.eof())
		{
			in>>dummy;
			mymap[var_id[count]].push_back(dummy);
			if(count==ncols-1)count=0;else count++;
		}
		mymap[var_id[0]].pop_back();
		return 0;
	}
	static map <const string, vector<double> >  read_vector_map (string root, string name,string suffix)
	{
		int col,ncols,count;
		double dummy;
		vector<string> var_id;
		string file;
		string descript;
                map <const string, vector<double> > mymap;
		file= root+name+suffix;

		// clear the map
		mymap.clear();
			
		//start to read
		ifstream in(file.c_str());
		if(!in)  err::err2("Basics_map::map <const string, vector<double> >  read_vector_map: File does not exist:"+file);
		in>>ncols;
		for(col=0;col<ncols;col++)
		{
			in>>descript;
			var_id.push_back(descript);
			vector <double> vect;
			mymap[descript] = vect;
		}
		count=0;
		while(!in.eof())
		{
			in>>dummy;
			mymap[var_id[count]].push_back(dummy);
			if(count==ncols-1)count=0;else count++;
		}
		mymap[var_id[0]].pop_back();
		return mymap;
	}
};

#endif